package ha.web.servlet.game;

import ha.web.games_manager.GM;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WaitingPage
 */
@WebServlet("/WaitingPage")
public class WaitingPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WaitingPage() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		GM ins = GM.getInstance();
		String userName = request.getParameter("user");
		String userPass = request.getParameter("pass");
		int userId = ins.createNewUser(userName, userPass);
		if (!ins.IsUserAtDB(userId)) {
			ServletContext context = getServletContext();
			RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
			dispatcher.forward(request, response);
		}
	      Cookie userCookie = new Cookie("chinese_checkers", userName+":"+String.valueOf(userId));
	      response.addCookie(userCookie);
	     
	      ins.player_login(userId);
	      int gameId = ins.get_game_id(userId);
	      //check if user have a game - if so - load it if no - check waiting list
	      if (gameId>0) {
	    	  out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
	    	  out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
	    	  out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
	    	  out.print("<h2> Welcome " + userName + "<h2>");
	    	  out.print("<h2>Just a sec<br> please be patient<h2>");
	    	  //start function to check if current player turn:
	    	  out.print("<script src=\"in_game_check.js\" type=\"text/javascript\"></script>");
	    	  out.print("<script>short_wait()</script>");
	    	  out.print("</div>");
	    	  out.print("</BODY></HTML>");
	      }else{
	    	  out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
	    	  out.print("<HTML><HEAD><TITLE>game page</TITLE>");	
	    	  out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
	    	  out.print("<h2> Welcome " + userName + "<h2>");
	    	  out.print("<h2>waiting for other player to log in,<br> please be patient<h2>");
	    	  out.print("<div id = \"debug\"></div>");
	    	  //start function to check if current player turn:
	    	  out.print("<script src=\"in_game_check.js\" type=\"text/javascript\"></script>");
	    	  out.print("<script>check_if_in_game()</script>");
	    	  out.print("</div>");
	    	  out.print("</BODY></HTML>");
	      }
	}
}


